

import absorbRectPos from './absorbonrectpos.js'
import absorbCirclePos from './absorboncirclepos.js'
import absorbMultiPos from './absorbonmultipos.js'
import absorbArcPos from './absorbonarcpos.js'
import absorbPointPos from './absorbonpointpos.js'

import  {LGXEnums}  from '../../common/sysenum.js'
var posMode = LGXEnums.LGXPosMode;

import utilfuncs from '../../common/tools/utilfuncs.js'
var funcs = utilfuncs.UtilFuncs;

var AbsorbOnPathCoordPos = {}

AbsorbOnPathCoordPos.calculate = function (mousePt, destPos) {
    let absorbInfo = { dis: -1, pt: { x: 0, y: 0 } };
    for (let i = 0; i < destPos.pathLinkList.length; i++) {
        let pathLink = destPos.pathLinkList[i];
        this.calculateOnLink(pathLink, mousePt, absorbInfo);
    }
    return absorbInfo.pt;
}

AbsorbOnPathCoordPos.calculateOnLink = function (pathLink, mousePt, absorbInfo) {
    let stUnit = pathLink.getPathStartUnit();
    if (stUnit) {
        this.calculateOnUnit(stUnit, mousePt, absorbInfo);
    }
}

AbsorbOnPathCoordPos.calculateOnUnit = function (unit, mousePt, absorbInfo) {
    let pt = this.calculateSubUnit(mousePt, unit.coordDecs);
    if (pt) {
        let dis = funcs.distancePoint(pt, mousePt);
        if ((absorbInfo.dis < 0) || (dis >= 0 && absorbInfo.dis > dis)) {
            absorbInfo.dis = dis;
            absorbInfo.pt.x = pt.x;
            absorbInfo.pt.y = pt.y;
        }
    }
    if (unit.nextUnit) {
        this.calculateOnUnit(unit.nextUnit, mousePt, absorbInfo);
    }
}



AbsorbOnPathCoordPos.calculateSubUnit = function (mousePt, destPos) {
    var pt = null;
    if (destPos.posType == posMode.circle_mode) {
        pt = absorbCirclePos.AbsorbCirclePos.calculate(mousePt, destPos);
    }
    else if (destPos.posType == posMode.rect_mode) {
        pt = absorbRectPos.AbsorbRectPos.calculate(mousePt, destPos);
    }
    else if (destPos.posType == posMode.multiPt_Mode) {
        pt = absorbMultiPos.AbsorbMultiPos.calculate(mousePt, destPos);
    }
    else if (destPos.posType == posMode.arc_mode) {
        pt = absorbArcPos.AbsorbArcPos.calculate(mousePt, destPos);
    }
    else if (destPos.posType == posMode.point_mode) {
        pt = absorbPointPos.AbsorbPointPos.calculate(mousePt, destPos);
    }
    return pt;
}

export default { AbsorbOnPathCoordPos }